#!/usr/bin/env python
import tab
import sys
import os
import commands
import random
import time
import string
#import a formating module named PrettyTable
from prettytable import PrettyTable

#define color for display
start_red = "\033[1;31m"
start_green = "\033[1;32m"
start_yellow = "\033[1;33m"
start_blue = "\033[1;34m"
end_color = "\033[0m"

class people:
    '''
    Class for people
    '''
    def __init__(self,name,age,sex,charm,lover,company,popo,salary = 0):
        self.name = name
        self.age = age
        self.sex = sex
        self.charm = charm
        self.salary = salary
        self.lover = lover
        self.company = company
        self.popo = popo

    def love(self):
        pass

    def study(self,school):
        print "=================== Start Learning ==================="
        for i in range(15):
            i += 1
            print "Study day %s......" % i
            john.charm += 1
            print "%sCharm + 1%s" %(start_green,end_color)
            time.sleep(1)
        else:
            print "%sCongratulations,You've graduated from Oldboy Python course.. Now you are a real technician!!!%s" %(start_yellow,end_color)

    def job_interview(self,company):
        if company == 'Nokia':
            print '''
%sInterviewer%s : I'm so sorry, you come too late, we've already been bankrupt...
            ''' %(start_blue,end_color)
            raw_input('%sDamn, What the hell happened..Whatever, to find a better one, Press any key to continue :%s' %(start_green,end_color)).strip()
        if company == '360':
            print '''
%sDoctor Yang%s : Welcome to 360, We need an Operation engineer with great programming skills.
                Could you introduce yourself first?
            ''' %(start_blue,end_color)
            while True:
                print '''
    ===================================================
    1. Introduce the last job
    2. Introduce the experience in Oldboy Python course
    ===================================================
                '''
                select = raw_input('%sPlease select a number above :%s' %(start_green,end_color)).strip()
                if select == '1':
                    print '''
%sJohn Berry%s : My last job is in a internet bar as a network Administrator.. b la b la la....
%sDoctor Yang%s : OK, I see, I think these experience is not enough to make you get this job...
                    Do you have any other experience?
                    ''' %(start_yellow,end_color,start_blue,end_color)
                elif select == '2':
                    print '''
%sJohn Berry%s : I just graduated from Oldboy Education Center, Studying Python with teacher Alex..
%sDoctor Yang%s : Alex!!! one second.. you mean teacher Alex in Oldboy? Are you sure about that?
%sJohn Berry%s : Yeah, I'm pretty sure.. you know him?
%sDoctor Yang%s : Exactly, Alex and I are not ordinary relationship,You know, HeHe.. You've got this offer!!!
                    ''' %(start_yellow,end_color,start_blue,end_color,start_yellow,end_color,start_blue,end_color)
                    break

    def working(self,company,years):
        if company == 'Internet Bar':
            print '''
    Please choose a kind of work:
    1. Making cables
    2. Installing system
    3. Repairing computer
            '''
            select = raw_input("%sChoose one :%s" %(start_green,end_color)).strip()
            if select == '1':
                print "%sBoss%s : John, You've made over 3000 cables in a year, Good job. Salary + 100!!!" %(start_yellow,end_color)
                john.salary += 100
                print "%sYour current salary is %s...%s" %(start_yellow,john.salary,end_color)
                years += 1
                return years
            elif select == '2':
                print "%sBoss%s : John, You've installed windows for over 300 times in a year, Good job. Salary + 100!!!" %(start_yellow,end_color)
                john.salary += 100
                print "%sYour current salary is %s...%s" %(start_yellow,john.salary,end_color)
                years += 1
                return years
            elif select == '3':
                print "%sBoss%s : John, You've repaired 150 computers in a year, Good job. Salary + 100!!!" %(start_yellow,end_color)
                john.salary += 100
                print "%sYour current salary is %s...%s" %(start_yellow,john.salary,end_color)
                years += 1
                return years
            else:
                return years

    def change_work(self):
        pass

    def get_promotion(self):
        pass

    def change_lover(self):
        pass

class company:
    '''
    Class for companys
    '''
    def __init__(self,name,positon,salary):
        self.name = name
        self.positon = positon
        self.salary = salary
    def offer(self):
        pass

class school:
    '''
    Class for school
    '''
    def __init__(self,name,teacher,site,course,tuition):
        self.name = name
        self.teacher = teacher
        self.site = site
        self.course = course
        self.tuition = tuition
    def teaching(self):
        pass

def Ptt(table_type):
    #for
    if table_type == 'status':
        table_type = PrettyTable(['Name','Age','Sex','Charm','Lover','Company','Positon','Salary'])
        table_type.align["Name"] = "l" # Left align city names
        table_type.padding_width = 1 # One space between column edges and contents (default)
        return table_type
    #for
    if table_type == 'story':
        table_type = PrettyTable(['Num','Selection'])
        table_type.align["Name"] = "l" # Left align city names
        table_type.align["Selection"] = "l" # Left align selection
        table_type.padding_width = 1 # One space between column edges and contents (default)
        return table_type

def view_bar(num=1, sum=100, bar_word=":"):
    rate = float(num) / float(sum)
    rate_num = int(rate * 100)
    print '%s\r%d%%%s :' %(start_yellow,rate_num,end_color),
    for i in range(0, num):
        os.write(1, bar_word)
    sys.stdout.flush()

def openning():
    print '''
    ===========================================================================
        %sHi, I'm very happy that you are ready to play this fantastic game.
    Let me tell you something about the background of this game. The role
    you are acting is John Berry, You just graduated from high school with
    your girl friend Liz, She's very pretty, you had a deal to study in the
    college together, Unfortunately, you didn't pass the final exam, however
    Liz did, So you are very sad, and you don't exactly wanna leave her as well.
    %s%sThe story is starting from this point.......%s
    ===========================================================================
    ''' %(start_blue,end_color,start_yellow,end_color)

def initial():
    while True:
        charm_init = random.randint(30,60)
        salary_init = random.randint(2000,3000)
        print '''
    ++++++++++++++++++++++++++++++++++++
    The following is your initial status:
    Name : John Berry
    Charm : %d
    Salary : %d
    ++++++++++++++++++++++++++++++++++++
        ''' % (charm_init,salary_init)
        select = raw_input('%sIf satisfied, Press y to continue, or n to reproduce the values :%s' %(start_yellow,end_color)).strip()
        if select.lower() == 'y' or len(select) == 0:
            return charm_init,salary_init
        else:
            continue

def Status():
    status = Ptt('status')
    status.add_row([john.name,john.age,john.sex,john.charm,john.lover,john.company,john.popo,john.salary])
    status.add_row([Liz.name,Liz.age,Liz.sex,Liz.charm,Liz.lover,Liz.company,Liz.popo,Liz.salary])
    print status

def Continue(Phase):
    while True:
            print '''
    ============================
    1. Check status
    2. Proceed to the next phase
    ============================
            '''
            select = raw_input('%sPlease select a number above :%s' %(start_green,end_color)).strip()
            if select == '1':
                Status()
            elif select == '2':
                Phase += 1
                return Phase

###################################################################################

if __name__ == '__main__':
    select = raw_input("%sAre you ready to play this game? Press any key to continue :%s" %(start_green,end_color)).strip()
    for i in range(0, 101):
        time.sleep(0.05)
        view_bar(i, 100)
    openning()
    result = initial()
    charm_init = result[0]
    salary_init = result[1]
    Phase = 1
    john = people('John Berry',18,'male',charm_init,'Liz','','student',salary_init)
    Liz = people('Liz',18,'female',80,'John Berry','','student',0)
    Phase = Continue(Phase)
    if Phase == 2:
        print '''
%sLiz%s : John, I also don't wanna leave you honey, but I have to study in another city.
      Would you like to move with me?, that will solve the problem.
        ''' %(start_red,end_color)
        while True:
            print '''
    1. Yes
    2. No
        '''
            select = raw_input('%sPlease select a number above :%s' %(start_green,end_color)).strip()
            if select == '1':
                print '%sJohn Berry%s : Of course, Honey, That\'s what I\'m thinking, even I can help you earn the tuition.' %(start_blue,end_color)
                print '%sLiz%s : Thanks for your understanding, I love you.' %(start_red,end_color)
                Phase += 1
                break
            elif select == '2':
                print 'In John\'s mind : %s(I cannot do this, I need re-selection...)%s' %(start_blue,end_color)
                continue
    if Phase == 3:
        net_bar = company('Internet Bar','Network Administrator',john.salary)
        years = 0
        print '''
%sJohn Berry%s : Liz, There is a good news for us, I\'ve got a job in the city of your college,I\'ll work for
            a %s as a %s , Salary is %s per month. I'm sure I can afford your tuition.
            Is it great?
%sLiz%s : That's great, you are quite a guy, John.
        ''' %(start_blue,end_color,net_bar.name,net_bar.positon,net_bar.salary,start_red,end_color)
        john.company = net_bar.name
        john.popo = net_bar.positon
        raw_input('%sPress any key to continue :%s' %(start_green,end_color))
        print '''
=======================================================================
In John's mind: %s(Ok, get started to earn money for Liz, Keep going!!!)%s
        ''' %(start_blue,end_color)
        while True:
            if years < 4:
                result = john.working(net_bar.name,years)
                years = result
            else:
                john.age += 4
                Liz.age += 4
                break
        print '''
    %s%s years past, Liz has graduated from college, %s almost pay for all tuition of Liz,
so, No money left. Liz got an offer from IBM, %s is still a network Administrator in a
small %s, one day, %s get a bad news from Liz....%s
        ''' %(start_blue,years,john.name,john.name,net_bar.name,john.name,end_color)
        Phase = Continue(Phase)
        print Phase
    if Phase == 4:
        print '''
%sLiz%s : John, I'm sorry, I'll break up with you.
%sJohn Berry%s : Why? tell me what happened..
%sLiz%s : I got a new boyfriend Peter, He is a sales manager in IBM, and a very nice guy,
    So, see you John, hope you happiness and find your real miss perfect...
        ''' %(start_red,end_color,start_blue,end_color,start_red,end_color)
        Liz.lover = 'Peter'
        john.lover = ''
        Liz.company = 'IBM'
        Liz.popo = 'Secretary'
        Liz.salary = 6000
        raw_input('%sPress any key to continue :%s' %(start_green,end_color))
        print '''
    Even though John is very sad, at the same time, John realizes that he must make changes,
First of all, He can't be only a network Administrator any more, He need to study, to study
something new, some technology can make him stronger and more valuable.. That's the point.
If I can get a big self promotion, Might Liz will be back around me.
        '''
        john.company = ''
        john.popo = ''
        Phase = Continue(Phase)
    if Phase == 5:
        oldboy = school('Oldboy','Alex','Shahe','Python',6000)
        zhonggu = school('Zhonggu','Max','Zhongguancun','Python',12000)
        while True:
            print '''
    John find python is very popular
    ================================
    1. Go to Oldboy Education Center
    2. Go to Zhonggu Education Center
    ================================
            '''
            select = raw_input('%sPlease select a number above :%s' %(start_green,end_color)).strip()
            if select == '1':
                print '''
%sAlex%s : Welcome to %s, The course: %s is a great language, It can help you do anything b la b la .....
        1 hour past.......
        The tuition is only %s, Is it unbelievable? Yes, only %s, Please believe what you heard, b la b la....
        1 hour past.......
        Do you wanna enroll?
                ''' %(start_green,end_color,oldboy.name,oldboy.course,oldboy.tuition,oldboy.tuition)
                raw_input('%sSounds perfect, No reason to reject!!! Press any key to continue :%s' %(start_green,end_color))
                john.study(oldboy.name)
                break
            elif select == '2':
                print '''
%sMax%s : Hey, Man, Here is %s, The course: %s, Tuition : %s, We are located in %s,
          Do you wanna enroll?
                ''' %(start_blue,end_color,zhonggu.name,zhonggu.course,zhonggu.tuition,zhonggu.site)
                raw_input("%sIt's too expensive, I'm unable to afford it. Let me find another one.. Press any key to continue :%s" %(start_green,end_color))
        Phase = Continue(Phase)
    if Phase == 6:
        qihu360 = company('360','Senior Operation engineer',12000)
        print '''
In John's mind : %s(That's perfectly ready to start finding a new job, GO!!!)%s
        ''' %(start_blue,end_color)
        while True:
            print '''
    =========================================
    1. Got an job interview from Nokia, Go!!!
    2. Got an job interview from 360, Go !!!
    =========================================
            '''
            select = raw_input('%sPlease select a number above :%s' %(start_green,end_color)).strip()
            if select == '1':
                john.job_interview('Nokia')
            elif select == '2':
                john.job_interview('360')
                john.company = qihu360.name
                john.popo = qihu360.positon
                john.salary = qihu360.salary
                print "John Berry got this job, Company: %s Positon: %s Salary: %s" %(qihu360.name,qihu360.positon,qihu360.salary)
                break
        Phase = Continue(Phase)
    if Phase == 7:
        while True:
            lack = 90 - john.charm
            if john.charm >= 90:
                print '''
------------------------------------------------------------------------------------------------------
%sCEO Zhou%s : %s you've finished lots of outstanding work these years, So, Company decides
            You will be the next CTO of 360, Congratulations!!!
%sJohn Berry%s : It's my great honor, I really appreciate it, and I must do my best to work for 360!!!
                ''' %(start_blue,end_color,john.name,start_yellow,end_color)
                john.popo = 'CTO'
                john.salary = 50000
                break
            else:
                print '''
    %sStart working in 360, You only need %s more values of Charm to get promoted!!!%s
    %sSelect a project to finish and increase your Charm:%s
    ==============================================================
    1. Deploy an strong Platform for monitoring %s(Charm + 10)%s
    2. Develop a Platform for automatic deployment %s(Charm + 20)%s
    3. Design and build automatic Operation system for 360 website %s(Charm + 30)%s
    ==============================================================
                ''' %(start_yellow,lack,end_color,start_green,end_color,start_blue,end_color,start_blue,end_color,start_blue,end_color)
                select = raw_input('%sPlease select a number above :%s' %(start_green,end_color)).strip()
                if select == '1':
                    print '''
%sBoss%s : Good job, %s, This monitor system is pretty good, You got a big achivement this year...
                    ''' %(start_blue,end_color,john.name)
                    john.charm += 10
                    john.age += 1
                    Liz.age += 1
                elif select == '2':
                    print '''
%sBoss%s : Good job, %s, This Platform you develped is great, You got a high score this year...
                    ''' %(start_blue,end_color,john.name)
                    john.charm += 20
                    john.age += 1
                    Liz.age += 1
                elif select == '3':
                    print '''
%sBoss%s : Good job, %s, This automatic Operation system is fantastic, You got a outstanding KPI score this year...
                    ''' %(start_blue,end_color,john.name)
                    john.charm += 30
                    john.age += 1
                    Liz.age += 1
        Phase = Continue(Phase)
    if Phase == 8:
        print '''
%sLiz%s : Hi, John. I'm back... Peter dumped me. I find that only you cherish me, and I still love you.
      Will you forgive me? I hope I could be with you in the rest of my life... Give me another opportunity please..
        ''' %(start_red,end_color)
        while True:
            print '''
        %sMaking your choise, This is a big time in your life%s
        =========================================
        1. Give her an opportunity and forgive her
        2. No way!!!
        3. Say something to her...
        =========================================
                ''' %(start_yellow,end_color)
            select = raw_input('%sPlease select a number above :%s' %(start_green,end_color)).strip()
            if select == '1':
                print '''
%sJohn Berry%s : Well, I still love you too, I'll be with you anytime.
                ''' %(start_blue,end_color)
                break
            elif select == '2':
                print '''
%sJohn Berry%s : You wish!!! Our relationship is the end. It's not possible to recover..
                ''' %(start_blue,end_color)
                break
            elif select == '3':
                sentence = raw_input('Type some words to Liz: ').strip()
                print sentence
                break











